package com.keyancompany.zhongbaopingtai.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.text.TextUtils;

public class DateUtils {

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String TXT_DEFAULT_FORMAT = "dd天HH时mm分ss秒";
    public static SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("yyyy-MM-dd ");
    static SimpleDateFormat DAY_HOUR_FORMAT = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");
    static SimpleDateFormat HOUR_FORMAT = new SimpleDateFormat(
            "HH:mm");
    static SimpleDateFormat TXT_DAY_HOUR_FORMAT = new SimpleDateFormat(
            "HH时mm分ss秒");

    /**
     * 是否为同一天
     *
     * @param firstTime
     * @param secTime
     * @return
     */
    public static boolean equals(long firstTime, long secTime) {
        return DAY_FORMAT.format(new Date(firstTime)).equals(
                DAY_FORMAT.format(new Date(secTime)));
    }

    /**
     * 是否为当天
     *
     * @param time
     * @return
     */
    public static boolean isToday(long time) {
        return DAY_FORMAT.format(new Date(time)).equals(
                DAY_FORMAT.format(new Date()));
    }

    public static String dateToRelativeTime(Date date) {
        if (DAY_FORMAT.format(date).equals(DAY_FORMAT.format(new Date()))) { // 今天
            return "今天 ".concat(HOUR_FORMAT.format(date));

        } else { //
            return DAY_HOUR_FORMAT.format(date);
        }

    }

    public static String dateToRelativeTime(long time) {

        return dateToRelativeTime(new Date(time));

    }

    public static String dateToRelativeTime(String time) {
        if (time == null || "".equals(time)) {
            return "";
        }
        try {
            Date date = DAY_HOUR_FORMAT.parse(time);
            // SimpleDateFormat sdf=new
            // SimpleDateFormat("yyyy-MM-dd HH:mm");//小写的mm表示的是分钟
            // java.util.Date date=sdf.parse(time);
            return dateToRelativeTime(date);
        } catch (Exception e) {
            return time;
        }

    }

    public static String formatDate(long time) {

        return DAY_FORMAT.format(new Date(time));
    }

    public static String formatDate(String format, long time) {
        Date date = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static String formatDate(Date time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_FORMAT);
        return simpleDateFormat.format(time);
    }

    public static String formatDayDate(Date time) {
        if (time == null)
            return "";
        return DAY_FORMAT.format(time);
    }

    public static Date stringToDate(String strTime) {
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_FORMAT);
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    public static Date stringToDate(String strTime, String format) {
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    public static Long stringToLong(String time) {
        String t = null;
        if (time != null) {
            if (time.length() > 19) {
                t = time.substring(0, 19);
            } else {
                t = time;
            }
        }
        if (t != null) {
            System.out.println("--------t----" + t);
            return stringToDate(t).getTime();
        }
        return null;
    }

    public static String getCurDateStr() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
                + c.get(Calendar.DAY_OF_MONTH) + " "
                + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)
                + ":" + c.get(Calendar.SECOND);
    }

    public static Date getCurDate() {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
        try {
            Date now = new Date();
            String test = sdf.format(now).toString();
            return sdf.parse(test);
        } catch (Exception e) {
            return null;
        }
    }


    @SuppressWarnings("deprecation")
    public static String getFromData(String time) {
        Calendar calendar = Calendar.getInstance();
        try {
            if (!TextUtils.isEmpty(time)) {
                calendar.setTime(DAY_FORMAT.parse(time));
            }
            return DAY_FORMAT.format(calendar.getTime());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param data1
     * @param data2
     * @return data1 是否在 data 之前
     */
    public static boolean isCompareData(String data1, String data2) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT,
                Locale.CHINA);
        try {
            Date d1 = sdf.parse(data1);
            Date d2 = sdf.parse(data2);
            return d1.before(d2);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    public static String currentData() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TXT_DEFAULT_FORMAT);
        return simpleDateFormat.format(new Date());
    }


    /**
     * 毫秒转日期
     *
     * @param time
     * @param TYPE_FORMAT
     * @return
     */
    public static String MsToDate(Long time, String TYPE_FORMAT) {
        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(TYPE_FORMAT);
        return sdf.format(d).toString();
    }

    /*
   * 毫秒转化时分秒毫秒
   */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append((day > 9 ? day : "0" + day) + "天");
        } else
            sb.append("00天");
        if (hour > 0) {
            sb.append((hour > 9 ? hour : "0" + hour) + "时");
        } else
            sb.append("00时");
        if (minute > 0) {
            sb.append((minute > 9 ? minute : "0" + minute) + "分");
        } else
            sb.append("00分");
        if (second > 0) {
            sb.append((second > 9 ? second : "0" + second) + "秒");
        } else
            sb.append("00秒");
        return sb.toString();
    }

    static {
        TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
        TimeZone.setDefault(tz);
    }


    public static int Currentweek() {//当前时间是第几周
        TimeZone zone = TimeZone.getTimeZone("Asia/Shanghai");
        Calendar cal = Calendar.getInstance(zone);
        int c = cal.get(cal.WEEK_OF_YEAR);
        return c;
    }

    public static int CurrentYear() {//当前时间是xx年
        Calendar a = Calendar.getInstance();
        System.out.println(a.get(Calendar.YEAR));//得到年
        int year = a.get(Calendar.YEAR);
        return year;
    }

    public static String CurrentTime() { //得到long类型当前时间
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(date);
        return time;
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
